import sys
import cv2

reload(sys)
sys.setdefaultencoding('utf8')

from PIL import Image 
import requests
import cv2

def download_img (url):
    res = requests.get('http://images.xxx.com/-7c0dc4dbdca3.webp', stream=True)  
    byte_stream = io.BytesIO(res.content) 
    roiImg = Image.open(byte_stream)  
    imgByteArr = io.BytesIO() 
    roiImg.save(imgByteArr, format='PNG') 
    imgByteArr = imgByteArr.getvalue() 
    return imgByteArr

# def array_to_image (data):
# 	    data
# 		for index in range(data):
# 			a = arr[index]
# 			r = PIL.Image.fromarray(a[0]).convert('L')
# 			g = PIL.Image.fromarray(a[1]).convert('L')
# 			b = PIL.Image.fromarray(a[2]).convert('L')
# 			image = PIL.Image.merge("RGB",(r,g,b))
#         return 

#imagepath = "/app/test/test.png"
imagepath = "https://static.leiphone.com/uploads/new/article/740_740/201704/5903040bf4146.jpg?imageMogr2/format/jpg/quality/90"

image = download_img(imagepath)
# image = cv2.imread(imagepath)
# cv2.imwrite('/app/test/copy.png',image)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# find
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load('/app/opencv/opencv-3.4.3/data/haarcascades/haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(
    gray,
    scaleFactor = 1.15,
    minNeighbors = 5,
    minSize = (5,5),
    flags = cv2.CASCADE_SCALE_IMAGE
)
print ("find {0} face".format(len(faces)))
